package com.lanou.dllo.findfood.activity;

import android.app.Activity;
import android.content.Intent;
import android.os.Bundle;
import android.text.method.HideReturnsTransformationMethod;
import android.text.method.PasswordTransformationMethod;
import android.util.Log;
import android.view.View;
import android.view.ViewGroup;
import android.widget.EditText;
import android.widget.ImageView;
import android.widget.TextView;

import com.lanou.dllo.findfood.R;
import com.lanou.dllo.findfood.database.UserDataBaseUtil;
import com.lanou.dllo.findfood.entity.User;
import com.lanou.dllo.findfood.tools.ToastCommon;

import java.util.ArrayList;
import java.util.List;

/**
 * Created by dllo on 15/10/29.
 */
public class RegisteredActivity extends Activity {
    private TextView register,protocol;
    private EditText pasw, phoneNumber,paswageen;
    private UserDataBaseUtil util;
    private ImageView eye,eyeIv,back;
    private static Boolean showPassword = false;
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);

        setContentView(R.layout.activity_regster);
        back = (ImageView) findViewById(R.id.backIv);
        back.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                RegisteredActivity.this.finish();
            }
        });
        register = (TextView) findViewById(R.id.gotoRegister);
        paswageen = (EditText) findViewById(R.id.paswageen);
        pasw = (EditText) findViewById(R.id.pasw);
        phoneNumber = (EditText) findViewById(R.id.phoneNumber);
        eye = (ImageView) findViewById(R.id.eye);
        eyeIv = (ImageView) findViewById(R.id.eyeIv);
        eyeIv.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                if(showPassword){//显示密码
                    showPassword = !showPassword;
                    paswageen.setTransformationMethod(HideReturnsTransformationMethod.getInstance());
                    paswageen.setSelection(paswageen.getText().toString().length());
                }else{//隐藏密码
                    showPassword = !showPassword;
                    paswageen.setTransformationMethod(PasswordTransformationMethod.getInstance());
                    paswageen.setSelection(paswageen.getText().toString().length());
                }
            }

        });
        eye.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {

                if(showPassword){//显示密码
                    showPassword = !showPassword;
                    pasw.setTransformationMethod(HideReturnsTransformationMethod.getInstance());
                    pasw.setSelection(pasw.getText().toString().length());
                }else{//隐藏密码
                    showPassword = !showPassword;

                    pasw.setTransformationMethod(PasswordTransformationMethod.getInstance());
                    pasw.setSelection(pasw.getText().toString().length());
                }
            }
        });

        util = new UserDataBaseUtil(this);
        register.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                saveData();

               // save();


            }
        });

    }


    public void saveData() {
        String passEt=paswageen.getText().toString();
        String name = phoneNumber.getText().toString();
        String password = pasw.getText().toString();
        List<User> users = new ArrayList<User>();
        User user = new User();
     //   List<User> userList = util.querUser();
         if ( name ==null) {
             ToastCommon.createToastConfig().ToastShow(this, (ViewGroup)
                     findViewById(R.id.toast_layout_root), "用户名不能为空");
        }else if(name.length() < 6){
             ToastCommon.createToastConfig().ToastShow(this, (ViewGroup)
                     findViewById(R.id.toast_layout_root), "请输入6位以上的用户名");
        }
        else if(password.length() < 6){
             ToastCommon.createToastConfig().ToastShow(this, (ViewGroup)
                     findViewById(R.id.toast_layout_root), "请输入6位以上的密码");
        }else if(password == null){
             ToastCommon.createToastConfig().ToastShow(this, (ViewGroup)
                     findViewById(R.id.toast_layout_root), "密码不能为空,请输入密码");
        }else if(passEt == null){
             ToastCommon.createToastConfig().ToastShow(this, (ViewGroup)
                     findViewById(R.id.toast_layout_root), "请确认您的密码");
         }else  if(!passEt.equals(password)){
             ToastCommon.createToastConfig().ToastShow(this, (ViewGroup)
                     findViewById(R.id.toast_layout_root), "两次输入密码不匹配,请重新确认密码");
        }else  {
             user.setNumber(name);
        user.setPassword(password);
        users.add(user);
        util.saveUserData(users);
        Log.i("us","---------------------------");
        Intent intent = new Intent(this,LoginAvtivtiy.class);
        intent.putExtra("name", name);// 相当于是键值
        startActivityForResult(intent, 200);
        finish();

         }


    }

    public void save(){

        String name = phoneNumber.getText().toString();
        String password = pasw.getText().toString();
        List<User> users = new ArrayList<User>();
        User user = new User();
        user.setNumber(name);
        user.setPassword(password);
        users.add(user);
        util.saveUserData(users);
        Intent intent = new Intent(this,LoginAvtivtiy.class);
        intent.putExtra("name", name);// 相当于是键值
        startActivityForResult(intent, 200);
        RegisteredActivity.this.finish();
    }

}
